<?php

namespace app\backend\controller;

use app\common\model\IntegralLog;
use app\common\model\Money;
use app\common\model\Redeem as redeemModel;
use app\common\model\ApplicationRedeem;
use app\common\model\User;
use app\common\model\WalletLog;
use think\Controller;
use think\Validate;
use think\Request;
use think\facade\Env;

class Redeem extends Controller
{
    public function index()
    {
        return $this->fetch();
    }

    public function redeemList()
    {
        $limit = request()->param('limit');
        $list = redeemModel::order('id desc')->paginate($limit);
        $count = redeemModel::count();
        $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => $list->items()];
        return json($data);

    }
/*
** 添加
*/
    public function add_redeem(Request $request)
    {
        return $this->fetch();
    }
    //操作
    public function doadd(Request $request)
    {
        $info = $_POST;
        $validate = new \app\common\validate\Redeem;

        if (!$validate->check($info)) {
            $res['code'] = 0;
            $res['msg'] = $validate->getError();
            return $res;
        }
        $result = redeemModel::create($info);
        if($result){
            $data['code'] = 1;
            $data['msg'] = '添加成功';
        }else{
            $data['code'] = 0;
            $data['msg'] = '添加失败';
        }
        return $data;


    }
    //更新
    public function redeem_edit(Request $request)
    { if (request()->isPost()){ //搜索
        $info = $_POST;
        $validate = new \app\common\validate\Redeem;

        if (!$validate->check($info)) {
            $res['code'] = 0;
            $res['msg'] = $validate->getError();
            return $res;
        }
        $update = redeemModel::where('id', $info['id'])->update($info);
        if($update){
            $data['code'] = 1;
            $data['msg'] = '修改成功';
        }else{
            $data['code'] = 0;
            $data['msg'] = '修改失败';
        }
        return $data;
    }
        $id =  $request->param('id');
        if($id) {
            $info = redeemModel::where('id', $id)->find();
            $this->assign('info', $info);
            return $this->fetch();
        }
    }
    //删除
    public function redeem_delete(Request $request){
        if (request()->isPost()){
        $id = $_POST['id'];
            $res = redeemModel::where('id',$id)->delete();
            if($res){
                $data['code'] = 1;
                $data['msg'] = '修改成功';
            }else{
                $data['code'] = 0;
                $data['msg'] = '修改失败';
            }
            return $data;
        }
    }
    //积分兑换申请列表
    public function integral(){
        return $this->fetch();
    }
    public function integralList()
    {
        $limit = request()->param('limit');
        $find = request()->param('find');
        if($find){
            $list = ApplicationRedeem::where('user_id',$find)->order('status asc')->order('id desc')->paginate($limit);
            $count = ApplicationRedeem::where('user_id',$find)->count();

        }else{
            $list = ApplicationRedeem::order('status asc')->order('id desc')->paginate($limit);
            $count = ApplicationRedeem::count();
        }
        $status = ['未审核','审核成功','审核失败'];
        foreach($list as $key=>$value){
            $list[$key]['status1'] = $status[$list[$key]['status']];
        }
        if ($list) {
            $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => $list->items()];
            return json($data);
        } else {
            $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => array()];
            return json($data);
        }

    }
    //批量审核成功
    public function review_success(){
        $id = request()->param('id');
        foreach($id as $value){
            $info = ApplicationRedeem::where('id',$value)->find();
            if($info['status'] == 0){
                //积分兑换审核成功，改变状态,给用户添加积分
                $res = ApplicationRedeem::where('id',$value)->update(['status'=>1]);
                if($res){
                    $data = [
                        'userid' => $info['userid'],
                        'money' => $info['balance'],
                        'type' => 3,//余额兑换增加
                    ];
                    $result = WalletLog::create($data);
                    if($result){
                        Money::where('userid', $info['userid'])->setInc('balance',$info['balance']);
                    }
                }
            }else{
                $res = 1;
            }
        }
        return array('code'=>'1','msg'=>'操作成功');

    }
    //单条审核成功
    public function review_suc(){
        $id = request()->param('id');
        $info = ApplicationRedeem::where('id',$id)->find();
        if($info['status'] == 0){
            //积分兑换审核成功，改变状态,给用户添加余额
            $res = ApplicationRedeem::where('id',$id)->update(['status'=>1]);
            if($res){
                $data = [
                    'userid' => $info['userid'],
                    'money' => $info['balance'],
                    'type' => 3,//余额兑换增加
                ];
                $result = WalletLog::create($data);
                if($result){
                    Money::where('userid', $info['userid'])->setInc('balance',$info['balance']);
                }
                return array('status'=>'1','msg'=>'操作成功');
            }
        }
        return array('status'=>'0','msg'=>'操作失败');
    }
    //单条审核失败
    public function review_err(){
        $id = request()->param('id');
        $info = ApplicationRedeem::where('id',$id)->find();
        if($info['status'] == 0){
            //积分兑换失败，改变状态，返还积分到用户
            $res = ApplicationRedeem::where('id',$id)->update(['status'=>2]);
            if($res){
                //添加余额记录
                $data = [
                    'userid' => $info['userid'],
                    'num' => $info['integral'],
                    'type' => 3,//余额兑换失败返还
                ];
                $result = IntegralLog::create($data);
                if($result){
                    Money::where('userid', $info['userid'])->setInc('integral',$info['integral']);
                }
                return array('status'=>'1','msg'=>'操作成功');
            }else{
                return array('status'=>'0','msg'=>'操作失败');
            }
        }else{
            $res = 1;
        }
        return array('status'=>'1','msg'=>'操作成功');
    }
    public function integral_ranking(){
        return $this->fetch();
    }
    public function integral_paihang(){
        $limit = request()->param('limit');
        $list =  Money::alias('m')->Join('langjin_user u','m.userid = u.id')->field('m.userid,m.integral,u.mobile,u.nickname')->order('m.integral desc')->paginate($limit);
        $count = Money::count();
        $data = ['code' => 0, 'msg' => '用户列表', 'count' => $count, 'data' => $list->items()];
        return json($data);
    }
}
